package com.ehe.generator.util;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import static com.ehe.generator.util.DefinitionUtil.*;

/**
 * 自动生成模板代码，工具类
 * @author jerry
 */
public class GenerateTemplateCodeUtil {

    public GenerateTemplateCodeUtil(com.ehe.generator.util.GenerateParameter generateParameter) {
        TABLE_NAME = generateParameter.getTableName();
        CHANGE_SET_ID = generateParameter.getChangeSetId();
        CLASS_NAME = generateParameter.getClassName();
        CLASS_EXPLAIN = generateParameter.getClassExplain();
        PROJECT_NAME = generateParameter.getProjectName();
        DOC_AUTHOR = generateParameter.getDocAuthor();
        PROJECT_DIR = generateParameter.getProjectDir();
        PARENT_PROJECT_NAME = generateParameter.getParentProjectName();
        PROJECT_PACKAGE_NAME = StringUtils.hasText(generateParameter.getProjectPackageName()) ? generateParameter.getProjectPackageName() : PROJECT_NAME;
        PARENT_PROJECT_PACKAGE_NAME = StringUtils.hasText(generateParameter.getParentProjectPackageName()) ? generateParameter.getParentProjectPackageName() : PARENT_PROJECT_NAME;
        BASE_PACKAGE_PREFIX = generateParameter.getBasePackagePrefix();
        if (generateParameter.getControllerExtends() != null) {
            CONTROLLER_EXTENDS = generateParameter.getControllerExtends();
        }
    }

    /** 表名*/
    static String TABLE_NAME = "t_sys_timetable_template_detail";

    /** 创建liquibase xml 的id*/
    static String CHANGE_SET_ID = "20190325-003";

    /** 类名*/
    static String CLASS_NAME = "TimetableTemplateDetail";

    /** 类注释*/
    static String CLASS_EXPLAIN = "排班模板详情";

    /** 子工程名称*/
    static String PROJECT_NAME = "system";

    /** 注释 - 作者*/
    static String DOC_AUTHOR = "天蚕土豆不吃西红柿";

    // windows 写法 D:\\IdeaProjects\\his\\
    // Linux 写法 /home/jerry/IdeaProjects/his/
    /** 项目根目录 */
    static String PROJECT_DIR = "D:\\JayXia\\jz-his\\";

    /** 子项目前缀名称 */
    static String PARENT_PROJECT_NAME = "his";

    /** 项目包名 */
    static String PROJECT_PACKAGE_NAME = PROJECT_NAME;

    /** 父项目包名 */
    static String PARENT_PROJECT_PACKAGE_NAME = PARENT_PROJECT_NAME;

    /** 基础包名前缀 */
    static String BASE_PACKAGE_PREFIX = "com.ehe.";

    /** controller 是否继承 superController */
    static boolean CONTROLLER_EXTENDS = true;

    /** 生成 domain 代码 */
    public void generateDomain() throws Exception {
        generateJavaFile(PackageSuffix.DOMAIN);
    }

    /** 生成所有代码 */
    public void generateMain() throws Exception {
        generateJavaFile(PackageSuffix.VO);
        generateJavaFile(PackageSuffix.ADD);
        generateJavaFile(PackageSuffix.MODIFY);
        generateJavaFile(PackageSuffix.SEARCHABLE);
        generateJavaFile(PackageSuffix.MAPPER);
        generateXmlFile(PackageSuffix.MAPPER_XML);
        generateJavaFile(PackageSuffix.MAPPER_TEST);
        generateJavaFile(PackageSuffix.SERVICE);
        generateJavaFile(PackageSuffix.SERVICE_IMPL);
        generateJavaFile(PackageSuffix.SERVICE_TEST);
        generateJavaFile(PackageSuffix.CONTROLLER);
        //generateJavaFile(PackageSuffix.CONTROLLER_IMPL);
        generateJavaFile(PackageSuffix.CONTROLLER_TEST);
        readDomain();
    }

    /** 生成 vo 代码 */
    public void generateVO() throws Exception {
        generateJavaFile(PackageSuffix.VO);
    }

    /** 生成 add 代码 */
    public void generateAdd() throws Exception {
        generateJavaFile(PackageSuffix.ADD);
    }

    /** 生成 modify 代码 */
    public void generateModify() throws Exception {
        generateJavaFile(PackageSuffix.MODIFY);
    }

    /** 生成 searchable 代码 */
    public void generateSearchable() throws Exception {
        generateJavaFile(PackageSuffix.SEARCHABLE);
    }

    /** 生成 mapper 代码 */
    public void generateMapper() throws Exception {
        generateJavaFile(PackageSuffix.MAPPER);
    }

    /** 生成 mapperXml 代码 */
    public void generateMapperXml() throws Exception {
        //generateJavaFile(PackageSuffix.MAPPER_XML);
        generateJavaXmlFile(PackageSuffix.MAPPER_XML);
    }

    /** 生成 mapperTest 代码 */
    public void generateMapperTest() throws Exception {
        generateJavaFile(PackageSuffix.MAPPER_TEST);
    }

    /** 生成 service 代码 */
    public void generateService() throws Exception {
        generateJavaFile(PackageSuffix.SERVICE);
    }

    /** 生成 serviceImpl 代码 */
    public void generateServiceImpl() throws Exception {
        generateJavaFile(PackageSuffix.SERVICE_IMPL);
    }

    /** 生成 serviceTest 代码 */
    public void generateServiceTest() throws Exception {
        generateJavaFile(PackageSuffix.SERVICE_TEST);
    }

    /** 生成 controller 代码 */
    public void generateController() throws Exception {
        generateJavaFile(PackageSuffix.CONTROLLER);
    }

    /** 生成 controllerImpl 代码 */
    public void generateControllerImpl() throws Exception {
        generateJavaFile(PackageSuffix.CONTROLLER_IMPL);
    }

    /** 生成 controllerTest 代码 */
    public void generateControllerTest() throws Exception {
        generateJavaFile(PackageSuffix.CONTROLLER_TEST);
    }

    /** 生成 liquibase 代码 */
    public void readDomain() throws IOException, TemplateException {
        Template template = DefinitionUtil.getTemplate("liquibaseXml.ftl");
        Writer out = new StringWriter();
        Map<String, Object> map = new HashMap<>(10);
        map.put("DOC_AUTHOR", DOC_AUTHOR);
        map.put("TABLE_NAME", TABLE_NAME);
        map.put("CHANGE_SET_ID", CHANGE_SET_ID);
        map.put(DefinitionUtil.PackageSuffix.DOMAIN.name(), DefinitionUtil.PackageSuffix.DOMAIN);
        map.put("FIELD_LIST", FieldParam.getList());
        template.process(map, out);
        System.out.println(out.toString());
    }

}
